package ${basePackage}.controller;

import com.github.pagehelper.PageInfo;
import ${common}.core.ApiResult;
import ${common}.core.PageResult;
import ${common}.util.BindingResultUtil;
import ${basePackage}.service.${tableInfo.className}Service;
import ${basePackage}.vo.entity.${tableInfo.className}EntityVO;
import ${basePackage}.vo.request.${tableInfo.className}RequestVO;
import ${basePackage}.vo.response.${tableInfo.className}ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * ${tableInfo.tableComment}
 *
 * @author ${author}
 * @date ${date}
 **/
@Slf4j
@RestController
@RequestMapping("${baseRequestMapping}")
@Api(description = "${tableInfo.tableComment}")
public class ${tableInfo.className}Controller {

    @Autowired
    private ${tableInfo.className}Service ${tableInfo.varName}Service;

    @ApiOperation("获取${tableInfo.tableComment}分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/${tableInfo.varName}", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<PageInfo<${tableInfo.className}ResponseVO>>> get${tableInfo.className}PageList(${tableInfo.className}RequestVO requestVO) {
        try {
            PageInfo<${tableInfo.className}ResponseVO> pageList = this.${tableInfo.varName}Service.query${tableInfo.className}PageList(requestVO);
            return ApiResult.ok(pageList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("获取${tableInfo.tableComment}信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/${tableInfo.varName}/{id}", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<${tableInfo.className}EntityVO>> get${tableInfo.className}(@PathVariable("id") String id) {
        try {
            ${tableInfo.className}EntityVO ${tableInfo.varName}EntityVO = this.${tableInfo.varName}Service.get${tableInfo.className}EntityVO(id);
            return ApiResult.ok(${tableInfo.varName}EntityVO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("新增${tableInfo.tableComment}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/${tableInfo.varName}", method = RequestMethod.POST)
    public ResponseEntity<ApiResult<Object>> add${tableInfo.className}(@RequestBody @Validated ${tableInfo.className}EntityVO entityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            this.${tableInfo.varName}Service.add${tableInfo.className}(entityVO);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("更新${tableInfo.tableComment}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/${tableInfo.varName}", method = RequestMethod.PUT)
    public ResponseEntity<ApiResult<Object>> update${tableInfo.className}(@RequestBody @Validated ${tableInfo.className}EntityVO entityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            this.${tableInfo.varName}Service.update${tableInfo.className}(entityVO);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

	<#if varNameList?seq_contains("useable")>
    @ApiOperation("启用${tableInfo.tableComment}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/${tableInfo.className}/enable/{id}", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<Object>> enable${tableInfo.className}(@PathVariable("id") String id) {
        try {
            this.${tableInfo.varName}Service.useable${tableInfo.className}(id, true);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("禁用${tableInfo.tableComment}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/${tableInfo.className}/disable/{id}", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<Object>> disable${tableInfo.className}(@PathVariable("id") String id) {
        try {
            this.${tableInfo.varName}Service.useable${tableInfo.className}(id, false);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }
	</#if>

    @ApiOperation("删除${tableInfo.tableComment}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/${tableInfo.varName}/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ApiResult<Object>> delete${tableInfo.className}(@PathVariable("id") String id) {
        try {
            this.${tableInfo.varName}Service.delete${tableInfo.className}(id);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }
}
